// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: MCZ Setup Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 40 : TYPE_MCZSETUP

// Function declarations
reserve function MCZSetup_Function97
reserve function MCZSetup_Function98
reserve function MCZSetup_Function99
reserve function MCZSetup_Function100

// Static Values

// Tables
public table MCZSetup_table38
	0xC00000, 0xE06000, 0xE0C000, 0xE0E080
end table


function MCZSetup_Function97
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			SwapMusicTrack("MysticCave_F.ogg", 0, 0x136D6, 0x1F40)
			break
		case 2
			SetMusicTrack("MysticCave_F.ogg", 0, 0x136D6)
			SwapMusicTrack("Invincibility_F.ogg", 2, 0x78B1, 0x1F40)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("MysticCave_F.ogg", 0, 0x136D6)
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			break
		end switch
	else
		stage.musicFlag = 1
	end if
end function


function MCZSetup_Function98
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			SwapMusicTrack("MysticCave.ogg", 0, 0x1841C, 0x30D4)
			break
		case 2
			SetMusicTrack("MysticCave.ogg", 0, 0x1841C)
			SwapMusicTrack("Invincibility.ogg", 2, 0x9717, 0x30D4)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("MysticCave.ogg", 0, 0x1841C)
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			break
		end switch
	else
		stage.musicFlag = 2
	end if
end function


function MCZSetup_Function99
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			SwapMusicTrack("MysticCave2_F.ogg", 0, 0x3BA8, 0x1F40)
			break
		case 2
			SetMusicTrack("MysticCave2_F.ogg", 0, 0x3BA8)
			SwapMusicTrack("Invincibility_F.ogg", 2, 0x78B1, 0x1F40)
			break
		case 4
			SetMusicTrack("MysticCave2_F.ogg", 0, 0x3BA8)
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			break
		end switch
	else
		stage.musicFlag = 1
	end if
end function


function MCZSetup_Function100
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			SwapMusicTrack("MysticCave2.ogg", 0, 0x48F0, 0x30D4)
			break
		case 2
			SetMusicTrack("MysticCave2.ogg", 0, 0x48F0)
			SwapMusicTrack("Invincibility.ogg", 2, 0x9717, 0x30D4)
			break
		case 4
			SetMusicTrack("MysticCave2.ogg", 0, 0x48F0)
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			break
		end switch
	else
		stage.musicFlag = 2
	end if
end function


function MCZSetup_Function101
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			SwapMusicTrack("MysticCaveAct2_F.ogg", 0, 1, 0x1F40)
			break
		case 2
			SetMusicTrack("MysticCaveAct2_F.ogg", 0, 1)
			SwapMusicTrack("Invincibility_F.ogg", 2, 0x78B1, 0x1F40)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("MysticCaveAct2_F.ogg", 0, 1)
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			break
		end switch
	else
		stage.musicFlag = 1
	end if
end function


function MCZSetup_Function102
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			SwapMusicTrack("MysticCaveAct2.ogg", 0, 1, 0x30D4)
			break
		case 2
			SetMusicTrack("MysticCaveAct2.ogg", 0, 1)
			SwapMusicTrack("Invincibility.ogg", 2, 0x9717, 0x30D4)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("MysticCaveAct2.ogg", 0, 1)
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			break
		end switch
	else
		stage.musicFlag = 2
	end if
end function


event ObjectMain
	object.value1++
	if object.value1 == 2
		object.value1 = 0
		object.value2++
		object.value2 &= 3
		GetTableValue(temp0, object.value2, MCZSetup_table38)
		SetPaletteEntry(0, 155, temp0)
	end if
end event


event ObjectStartup
	if options.vsMode == 0
		if saved.act2tracks == 0
			SetMusicTrack("MysticCave.ogg", 0, 0x1841C)
			SpeedUpMusic = MCZSetup_Function97
			SlowDownMusic = MCZSetup_Function98
		else
			if stage.actNum == 1
				SetMusicTrack("MysticCave.ogg", 0, 0x1841C)
				SpeedUpMusic = MCZSetup_Function97
				SlowDownMusic = MCZSetup_Function98
			end if
	
			if stage.actNum == 2
				SetMusicTrack("MysticCaveAct2.ogg", 0, 1)
				SpeedUpMusic = MCZSetup_Function101
				SlowDownMusic = MCZSetup_Function102
			end if
		end if
	else
		SetMusicTrack("MysticCave2.ogg", 0, 0x48F0)
		SpeedUpMusic = MCZSetup_Function99
		SlowDownMusic = MCZSetup_Function100
	end if
	SetPaletteEntry(0, 192, 0)
	animalType1 = TypeName[Cucky]
	animalType2 = TypeName[Mocky]
	object[10].type = TypeName[MCZ Setup]
	object[10].priority = PRIORITY_ACTIVE
	switch stage.actNum
	case 1
		ResetObjectEntity(244, TypeName[Blank Object], 0, 0, 0)
		stage.curYBoundary1 = 960
		tileLayer[1].scrollPos = -0x1400000
		tileLayer[1].parallaxFactor = 85
		SetTileLayerEntry(86, 0, 46, 14)
		break
	case 2
		stage.curYBoundary1 = 96
		tileLayer[1].scrollPos = -0x100000
		tileLayer[1].parallaxFactor = 42
		stage.curYBoundary2 = 0x800
		stage.deathBoundary = stage.curYBoundary2
		stage.deathBoundary <<= 16
		break
	end switch
	switch stage.actNum
		case 0
		case 1
			//LoadPalette("MCZAct1Pal.act", 0, 128, 128, 256)
			break
		case 2
			LoadPalette("MCZAct2Pal.act", 0, 128, 128, 256)
			break
	end switch
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
